/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : MadMinuteHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 2, 2009  Raghu Sarraff	 		        Created
 */
package net.lwf.handler.math.arithmatics;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

import net.lwf.collection.CollectionUtils;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.util.MathUtil;
import net.lwf.handler.util.SumGenerator;
import net.lwf.jaxb.entity.BaseData;
import net.lwf.jaxb.service.XMLDataService;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.entities.math.MadMinSum;
import lwf.handler.entities.math.SumInput;
import lwf.handler.helper.DisplayFormatter;


public class MadMinuteHandler extends ProcessHandler implements IProcessHandler {
	
	//private List data = null;
	private String grade;
	private String operation;
	private int digits;
	private int rownumber;
	private int sumnumber;
	private List<String> operations;

	public MadMinuteHandler(String grade) {
		this.grade = grade;
		XMLDataService service = new XMLDataService();
		this.operations = service.getOperations(Integer.parseInt(this.grade));
	}
	public MadMinuteHandler(String grade, String operation) {
		
		this.grade = grade;

		if(operation.equals("random")) {
			this.operation = null; //Random operation
		}
		else {
			this.operation = operation;
		}
		this.init();
		this.rownumber = 5;
		this.sumnumber = 5;
		////data = SumGenerator.generate(input);
	}
	
//	public List getData() {
//		SumInput input = new SumInput(this.operation);
//		input.setSumsperrow(this.sumnumber);
//		input.setRowsperpage(this.rownumber);
//		data = SumGenerator.generate2(input);
//		return data;
//	}

	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	public String getQuestionDesc() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		QuestionInfo qinfo = new QuestionInfo();
		//Create Input criteria
		SumInput input = new SumInput(this.operation);
		input.setDigits(this.digits);
		input.setSumsperrow(this.sumnumber);
		input.setRowsperpage(this.rownumber);
		input.setValidOperations(CollectionUtils.toMap(this.operations));
		//Get data
		List<MadMinSum> data = SumGenerator.generate2(input);
		//Set data
		qinfo.setQuestionObject(data);
		//qinfo.setPlaceHolder(placeHolder);
		return qinfo;
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
	private void init() {
	//private void init() {	//To go in a common super class
		XMLDataService service = new XMLDataService();
		BaseData data =  service.getArithmaticData(Integer.parseInt(this.grade), this.operation);

		this.operations = service.getOperations(Integer.parseInt(this.grade));
		
		if(data == null) {
			String randome_operation = MathUtil.getOperation();
			Map<String, String> map_validOperations = CollectionUtils.toMap(this.operations);
			while(!map_validOperations.containsKey(randome_operation)) {
				randome_operation = MathUtil.getOperation();
			}
			data =  service.getArithmaticData(Integer.parseInt(this.grade), randome_operation);
		}
		
		//Read the value by JAXB code - input = grade; output = digits, decimal points
		this.digits = data.getDigits();
	}
	
	public List<RadioData> getOperationsData() {
		List<RadioData> radiodata = new ArrayList<RadioData>();
		RadioData radio = null;
		for(String data: this.operations) {
			radio = new RadioData(data, data);
			radiodata.add(radio);
		}
		radio = new RadioData("random", "Random");
		radiodata.add(radio);
		return radiodata;
	}

} //MadMinuteHandler

